﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
}

<!DOCTYLE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <link href="~/libs/layui-v2.9.17/layui/css/layui.css" rel="stylesheet" />
</head>
<body>
    <form class="layui-form layui-row layui-col-space4">
        <div class="layui-form-item">
            <div class="layui-col-md4">
                <label class="layui-form-label">采购单号：</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" name="curCode" id="curCode" autocomplete="off" lay-affix="clear" class="layui-input">
                </div>
            </div>

            <div class="layui-col-md4">
                <label class="layui-form-label">产品编号：</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" name="proCode" id="proCode" autocomplete="off" lay-affix="clear" class="layui-input">
                </div>
            </div>

            <div class="layui-col-md4">
                <label class="layui-form-label">产品名称：</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" name="proName" id="proName" autocomplete="off" lay-affix="clear" class="layui-input">
                </div>
            </div>

            <div class="layui-btn-container layui-col-md4">
                <button class="layui-btn layui-btn-sm" lay-submit lay-filter="demo-table-search">查询</button>
                <button type="reset" class="layui-btn layui-btn-sm layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <table class="layui-hide" id="demo" lay-filter="test"></table>

    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
        <button type="button" class="layui-btn layui-btn-sm layui-btn-primary" lay-on="test-page-custom">
            添加
        </button>
        <button type="button" class="layui-btn layui-btn-sm layui-btn-warming" lay-event="updatemea">
            编辑
        </button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-submit id="deletes" lay-event="deletes">删除</button>
          </div>
    </script>
    <script type="text/html" id="toolDemo">
        <div class="layui-clear-space">
          <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="detail">查看</a>
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delmea">删除</a>
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delmea">退货</a>
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delmea">入账</a>
        </div>
    </script>
    <script src="~/libs/layui-v2.9.17/layui/layui.js"></script>
    <script src="~/libs/jquery/jquery.js"></script>

    <script>
        layui.use(['table'], function () {
            var table = layui.table;
            var form = layui.form;
            //显示
            ShowUnitInfo = function () {
                //执行一个 table 实例
                table.render({
                    elem: '#demo'
                    , url: '/XiangHe/GetProcument'  //数据接口
                    , title: '退货列表'
                    , cols: [[ //表头
                        { type: 'checkbox', fixed: 'left' },//锁定第一列,
                        { field: 'procurementCode', title: '订单号', width: 100 },
                        { field: 'productName', title: '产品名称', width: 100 },
                        { field: 'productCode', title: '产品编号', width: 100 },
                        { field: 'productSpecification', title: '产品规格', width: 100 },
                        { field: 'unit', title: '单位', width: 100 },
                        { field: 'unitPrice', title: '单价', width: 100 },
                        { field: 'quantity', title: '数量', width: 100 },
                        {
                            field: 'vendorName', title: '总额', width: 100, templet: function (d) { return (d.unitPrice * d.quantity).toFixed(2); }
                        },
                        { field: 'supplierCode', title: '供应商编号', width: 100 },
                        { field: 'supplierName', title: '供应商名称', width: 100 },
                        {
                            field: 'vendorName', title: '订单总额', width: 100, templet: function (d) { return (d.unitPrice * d.quantity).toFixed(2); }
                        },
                        { field: 'proStatus', title: '状态', width: 100 },
                        { field: 'isRu', title: '是否入账', width: 100 },
                        { field: 'isReturn', title: '退货', width: 100 },
                        { field: 'createDate', title: '创建时间', width: 180, templet: function (d) { return layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss'); } },
                        { field: 'right', title: '操作', width: 240, templet: "#toolDemo" }
                    ]],
                    page: {
                        limit: 10,
                        limits: [10, 20, 30, 40, 50],
                        prev: '上一页',
                        next: '下一页',
                        first: '首页',
                        last: '尾页'
                    },
                    request: {
                        pageName: 'pageIndex',
                        limitName: 'pageSize'
                    },
                    parseData: function (res) {
                        console.log(res.data)
                        return {
                            "code": res.code, //解析接口状态
                            "count": res.count, //解析数据长度
                            "data": res.data//解析数据列表
                        };
                    },
                    height: 525,
                    toolbar: '#toolbarDemo',
                });
            }
            ShowUnitInfo();
            // 搜索提交
            form.on('submit(demo-table-search)', function (data) {
                var field = data.field; // 获得表单字段
                // 执行搜索重载
                table.reload('demo', {
                    page: {
                        curr: 1 // 重新从第 1 页开始
                    },
                    where: field// 搜索的字段
                });
                layer.msg('搜索成功');
                return false; // 阻止默认 form 跳转
            });

            //添加
            layui.use(function () {
                var $ = layui.$;
                var layer = layui.layer;
                var util = layui.util;
                var form = layui.form;
                util.on('lay-on', {
                    'test-page-custom': function () {
                        //跳转到添加页面
                        window.location.href = "/XiangHe/PurAddView";
                        // layer.open({
                        //     type: 2,
                        //     area: ['800px', '750px'],
                        //     resize: false,
                        //     shadeClose: true,
                        //     title: '添加供应商退货',
                        //     content: `/XiangHe/PurAddView`,
                        //     btn: ['确定', '取消'],
                        //     yes: function (index, layero) {
                        //         //获取表单数据
                        //         var data = layer.getChildFrame('body', index).find('form').serialize();
                        //         console.log(data);
                        //         $.ajax({
                        //             url: "/XiangHe/AddProcure",
                        //             type: "get",
                        //             async: true,
                        //             data: data,
                        //             contentType: "application/x-www-form-urlencoded",
                        //             success: res => {
                        //                 if (res != null) {
                        //                     alert("添加成功");
                        //                     var index = parent.layer.getFrameIndex(window.name);
                        //                     parent.layer.close(index);
                        //                     layer.closeAll();//关闭弹出层
                        //                     ShowUnitInfo();//刷新表格
                        //                 }
                        //                 else {
                        //                     alert("添加失败")
                        //                 }
                        //             },
                        //         });
                        //         return false; // 阻止默认 form 跳转
                        //     },
                        // });
                    }
                })
            });

            // 工具栏事件 删除、修改
            table.on('toolbar(test)', function (obj) {
                var id = obj.config.id;
                var checkStatus = table.checkStatus(id);
                var othis = lay(this);
                switch (obj.event) {
                    //批删
                    case 'deletes':
                        var data = checkStatus.data;
                        if (data.length == 0) {
                            layer.msg('请选择一行');
                            return;
                        }
                        var ids = "";
                        for (var i = 0; i < data.length; i++) {
                            ids += data[i].id + ",";
                        }
                        layer.confirm("确定删除吗？", function (index) {
                            $.ajax({
                                url: "/XiangHe/DeleteReturnRange",
                                type: "get",
                                async: true,
                                data: { id: ids },
                                success: res => {
                                    if (res == 1) {
                                        layer.msg('删除成功');
                                        // 刷新页面
                                        table.reload('demo', {
                                            page: {
                                                curr: 1 // 重新从第 1 页开始
                                            }
                                        });
                                        return;
                                    } else {
                                        layer.msg('删除失败');
                                    }
                                }
                            });
                        });
                        break;
                    //修改
                    case 'updatemea':
                        var data = checkStatus.data;
                        if (data.length == 0) {
                            layer.msg('请选择一行');
                            return;
                        }
                        if (data.length > 1) {
                            layer.msg('只能修改一条');
                            return;
                        }
                        console.log(data)
                        layer.open({
                            type: 2,
                            area: ['600px', '500px'],
                            resize: false,
                            shadeClose: true,
                            title: '修改单位',
                            content: `/XiangHe/ProReeturnUpdateView?id=` + data[0].id,
                            btn: ['确定', '取消'],
                            yes: function (index, layero) {
                                // 获取弹出层中的表单数据
                                var data = layer.getChildFrame('body', index).find('form').serialize();
                                console.log(data)
                                // 发送Ajax请求
                                $.ajax({
                                    url: "/XiangHe/UpdateMeasure",
                                    type: "get",
                                    async: true,
                                    data: data,
                                    contentType: "application/x-www-form-urlencoded",
                                    success: res => {
                                        if (res != null) {
                                            alert("修改成功")
                                            //关闭弹出层
                                            layer.closeAll();
                                            //刷新表格
                                            table.reload('demo', {
                                                page: {
                                                    curr: 1 // 重新从第 1 页开始
                                                }
                                            });
                                            return;
                                        }
                                        else {
                                            console.log(res.code)
                                            alert("修改失败")
                                        }
                                    }
                                });
                                return false; // 阻止默认 form 跳转
                            },
                        });
                        break;
                };
            });

            //表单操作
            table.on('tool(test)', function (obj) {
                //修改
                if (obj.event === 'detail') {
                    var data = obj.data;
                    layer.open({
                        type: 2,
                        area: ['500px', '400px'],
                        resize: false,
                        shadeClose: true,
                        title: '修改退货',
                        content: `/XiangHe/ProReeturnUpdateView?id=${data.id}`,
                        btn: ['确定', '取消'],
                        yes: function (index, layero) {
                            var data = layer.getChildFrame('body', index).find('form').serialize();
                            console.log(data);
                            $.ajax({
                                url: "/XiangHe/UpdateReturnOrder",
                                type: "get",
                                async: true,
                                data: data,
                                contentType: "application/json",
                                success: res => {
                                    if (res == 1) {
                                        var index = parent.layer.getFrameIndex(window.name);
                                        parent.layer.close(index);
                                        alert("修改成功");
                                        layer.closeAll();//关闭弹出层
                                        ShowUnitInfo();//刷新表格
                                    }
                                    else {
                                        alert("修改失败")
                                    }
                                },
                            });
                            return false; // 阻止默认 form 跳转
                        },
                    });
                }
                //删除
                else if (obj.event === 'delmea') {
                    var data = obj.data//获取当前数据
                        , layEvent = obj.event;//获取lay-event获取对应的值
                    console.log(data)
                    layer.confirm('真的删除供应商退货吗？', function (index) {
                        $.ajax({
                            url: "/XiangHe/DeletReturnOrder?id=" + data.id,
                            type: "get",
                        });
                        alert("删除成功");
                        obj.del();
                        layer.close(index);
                    })
                }
            });
        });
    </script>
</body>
</html>
